package com.yyq.gxdc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyq.gxdc.bean.OrderVO;
import com.yyq.gxdc.bean.Result;
import com.yyq.gxdc.entity.DiningTable;
import com.yyq.gxdc.entity.OrderInfo;
import com.yyq.gxdc.entity.User;
import com.yyq.gxdc.mapper.DiningTableMapper;
import com.yyq.gxdc.service.ICartService;
import com.yyq.gxdc.service.IDiningTableService;
import com.yyq.gxdc.service.IOrderInfoService;
import com.yyq.gxdc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/orderInfo")
public class OrderInfoController {

    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private ICartService cartService;

    @Autowired
    private IUserService userService;

    @Autowired
    private DiningTableMapper diningTableMapper;

    /**
     * 分页查找 -- 根据当前登录人获取收货地址
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/front/orderInfoList")
    @ResponseBody
    public Result<IPage<OrderInfo>> orderInfoList(
            HttpServletRequest request,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10000") Integer pageSize,
            Model model) {

        Result<IPage<OrderInfo>> result = new Result<IPage<OrderInfo>>();
        Page<OrderInfo> page = new Page<OrderInfo>(pageNum, pageSize);
        OrderInfo orderInfo = new OrderInfo();
        // 获取当前登录人 信息
        Object curruser = request.getSession().getAttribute("user");
        User user = userService.getById((Integer)curruser);
        orderInfo.setUid(user.getId());
        IPage<OrderInfo> allCart = orderInfoService.getAllOrderInfo(page, orderInfo);
        result.setSuccess(true);
        result.setResult(allCart);
        return result;
    }


    /**
     * 后台分页查找 -- 根据当前登录人获取收货地址
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/mgr/orderInfoList")
    @ResponseBody
    public Result<IPage<OrderInfo>> mgrOrderInfoList(
            OrderInfo orderInfoParam,
            HttpServletRequest request,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10000") Integer pageSize,
            Model model) {

        Result<IPage<OrderInfo>> result = new Result<IPage<OrderInfo>>();
        Page<OrderInfo> page = new Page<OrderInfo>(pageNum, pageSize);
        OrderInfo orderInfo = new OrderInfo();
        IPage<OrderInfo> allCart = orderInfoService.getAllOrderInfo(page, orderInfo);
        result.setSuccess(true);
        result.setResult(allCart);
        return result;
    }

    /**
     * 添加订单管理信息
     * @param orderVO
     * @param req
     * @return
     */
    @PostMapping(value = "/addOrderInfo")
    @ResponseBody
    public Result<Object> addOrderInfo(OrderVO orderVO, HttpServletRequest req) {
        // 生成订单
        Date date = new Date();
        Object curruser = req.getSession().getAttribute("user");
        User user = userService.getById((Integer)curruser);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(date);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderNo(newDate);
        orderInfo.setUid(user.getId());
        orderInfo.setOrderPrice(orderVO.getPrice());
        orderInfo.setPreferentialPrice(orderVO.getPreferentialPrice());
        orderInfo.setTotalPrice(orderVO.getTotalPrice());
        orderInfo.setReceiver(user.getName());
        orderInfo.setDiningTable(orderVO.getDiningTable());
        orderInfo.setDiningTableId(orderVO.getDiningTableId());
        // 占用餐台
        DiningTable diningTable = diningTableMapper.selectById(orderInfo.getDiningTableId());
        if(diningTable.getIsUse().equals(1)){
            return Result.error(diningTable.getName()+"餐台正在使用中，请重新选择餐台！");
        }else {
            diningTable.setIsUse(1);
            diningTableMapper.updateById(diningTable);
        }
        orderInfo.setUsername(user.getName());
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime=format.format(date);
        orderInfo.setOrderTime(dateTime);
        orderInfoService.add(orderInfo);
        // 然后删除掉 传的购物车里面这些商品id
        List<Integer> dishList = orderVO.getDishList();
        for (Integer dishId:dishList) {
            cartService.deleteByUidAndDishId(user.getId(),dishId);
        }
        return Result.ok();
    }

    /**
     * 删除订单
     * @param orderInfo
     * @param req
     * @return
     */
    @PostMapping(value = "/deleteOrderInfo")
    @ResponseBody
    public Result<Object> deleteOrderInfo(OrderInfo orderInfo, HttpServletRequest req) {
        // 先查询 这个 购物车记录 是否已存在
        OrderInfo byId = orderInfoService.findById(orderInfo.getId());
        if(byId == null ){
            return Result.error("该地址不存在！");
        }else{
            orderInfoService.delete(orderInfo.getId());
            return Result.ok();
        }
    }



}
